package Servlet;

import javaBean.Message;
import useBean.MessageDB;
import useBean.UserDB;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>Title: UpdateMessageServlet</p>
 * <p>author 12427</p>
 *
 * @version ver1.0
 * @date 2021/12/24 12:54
 */
@WebServlet("/UpdateMessageServlet")
public class UpdateMessageServlet extends HttpServlet {
    /**
     * @see HttpServlet#doGet(HttpServletRequest, HttpServletResponse) (HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        String url ="/modify.jsp";

        try {
            ModifyMessage(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Message> mL = null;
        try {
            mL = UserDB.selectU();
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("MessageList", mL);
        getServletContext().getRequestDispatcher(url).forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        String action = request.getParameter("action");
        String url ="/MessageList.jsp";

        try {
            ModifyMessage(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<Message> mL = null;
        try {
            mL = UserDB.selectU();
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("MessageList", mL);
        request.getServletContext().getRequestDispatcher(url).forward(request, response);
    }

    private void ModifyMessage(HttpServletRequest request,HttpServletResponse response) throws Exception {
        String mid = request.getParameter("mid") ;
        String title = request.getParameter("title") ;
        String text = request.getParameter("context") ;

        MessageDB.modifyMessagem(mid ,  title , text);
    }
}